package pit.com.ordermobilesystem;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import pit.com.ordermobilesystem.BaseFragment.TypeLoadingMore;
import pit.com.ordermobilesystem.adapter.NewsAdapter;
import pit.com.ordermobilesystem.constant.NotificationType;
import pit.com.ordermobilesystem.http.HttpApiException;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import android.app.Notification;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class LoginActivity extends BaseFragmentActivity{
	EditText ed_username;
	EditText ed_pass;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_login);
	        getActionBar().hide();
	        ed_username=(EditText) findViewById(R.id.ed_username);
	        ed_pass=(EditText) findViewById(R.id.ed_pass);
	        Button bt_login=(Button) findViewById(R.id.bt_login);
	        bt_login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				new LoginTask(TypeLoadingMore.LOADING_FIRST).execute();
				
			}
	        });
			Button bt_skip=(Button) findViewById(R.id.bt_skip);
			bt_skip.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					finish();
					
				}
			});
	    }
	    
	    
	    
		class LoginTask extends AsyncTask<String, Void, Void> {

			TypeLoadingMore typeLoading;
			Exception exception;
			int response_code;
			JSONObject user;
			String username="";
			String password="";
			String data="";
			public LoginTask(TypeLoadingMore typeLoading) {
				super();
				this.typeLoading = typeLoading;
			}
			protected void onPreExecute() {
				if(typeLoading==TypeLoadingMore.LOADING_FIRST){
					showProgressDialog(new DialogInterface.OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface dialog) {
							LoginTask.this.cancel(true);
							dialog.dismiss();
						}


					});
				}
				username=ed_username.getText().toString();
				password=ed_pass.getText().toString();
	
				try {
					JSONObject object=new JSONObject();
					object.put("username", username);
					object.put("password", password);
					data=object.toString();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}


			}

			protected Void doInBackground(final String... args) {
				OrderSystemHttpService httpService=new OrderSystemHttpService(OrderSharePreference.getHost(LoginActivity.this),OrderSharePreference.getPort(LoginActivity.this));
				try {
					JSONObject result=httpService.requestToService(OrderSystemHttpService.URL_LOGIN, data);
					if(result.getString("status").equals("ok")){		
						user=result.getJSONObject("data").getJSONObject("user");
					}else{
						exception=new Exception(result.getString("error"));
					}

				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					exception=e;
				} catch (HttpApiException e) {
					// TODO Auto-generated catch block
					exception=e;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					exception=e;
				}
				return null;
			}

			protected void onPostExecute(final Void unused) {

					hideProgressDialog();

				
				if(exception!=null){
					showAlert("",exception.getMessage());
				}else{
					OrderSharePreference.setUser(getApplicationContext(), user.toString());
					Intent intent = new Intent();
					intent.setAction(NotificationType.LOGIN);
					intent.putExtra("data",getIntent().getStringExtra("data"));
					sendBroadcast(intent);					
					finish();
				}
				
			}

		}	    

	    
}
